package com.cloud.lawcase.api.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.cloud.lawcase.api.bean.LawCriteriaBean;
import com.cloud.lawcase.api.entity.TcDicLaw;
import com.cloud.lawcase.api.entity.TcDicLawCause;
import com.cloud.lawcase.api.entity.TcLawClDetail;
import com.cloud.lawcase.api.entity.TcLawRelCauseBiz;
import com.cloud.lawcase.api.entity.TcLawRelCauseExcuteType;
import com.cloud.lawcase.api.entity.TcLawRelCauseLawItem;
import com.cloud.lawcase.api.entity.TcLawRelLawApprovalDept;
import com.cloud.lawcase.api.entity.TcLawRelLawPublishDept;
import com.cloud.lawcase.api.entity.TcRelPunishUnitFunc;
import com.cloud.lawcase.api.entity.TcRelPunishUnitLevel;
import com.cloud.lawcase.api.mapper.TcDicLawCauseMapper;
import com.cloud.lawcase.api.mapper.TcDicLawMapper;
import com.cloud.lawcase.api.mapper.TcDicLawPublishDeptMapper;
import com.cloud.lawcase.api.mapper.TcLawClDetailMapper;
import com.cloud.lawcase.api.mapper.TcLawRelCauseBizMapper;
import com.cloud.lawcase.api.mapper.TcLawRelCauseExcuteTypeMapper;
import com.cloud.lawcase.api.mapper.TcLawRelCauseLawItemMapper;
import com.cloud.lawcase.api.mapper.TcLawRelLawApprovalDeptMapper;
import com.cloud.lawcase.api.mapper.TcLawRelLawPublishDeptMapper;
import com.cloud.lawcase.api.mapper.TcRelPunishUnitFuncMapper;
import com.cloud.lawcase.api.mapper.TcRelPunishUnitLevelMapper;
import com.cloud.lawcase.api.service.LawService;
import com.cloud.lawcase.common.utils.RedisKeyHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@Transactional
public class LawServiceImpl implements LawService {
	@Autowired
	private TcDicLawMapper tcDicLawMapper;
	@Autowired
	private TcDicLawPublishDeptMapper tcDicLawPublishDeptMapper;
	@Autowired
	private TcLawRelLawPublishDeptMapper tcLawRelLawPublishDeptMapper;
	@Autowired
	private TcLawRelLawApprovalDeptMapper tcLawRelLawApprovalDeptMapper;
	@Autowired
	private TcDicLawCauseMapper tcDicLawCaseMapper;
	@Autowired
	private TcLawRelCauseLawItemMapper tcLawRelCauseLawItemMapper;
	@Autowired
	private TcLawRelCauseBizMapper tcLawRelCauseBizMapper;
	@Autowired
	private TcLawRelCauseExcuteTypeMapper tcLawRelCauseExcuteTypeMapper;
	@Autowired
	private TcRelPunishUnitFuncMapper tcRelPunishUnitFuncMapper;
	@Autowired
	private TcRelPunishUnitLevelMapper tcRelPunishUnitLevelMapper;
	@Autowired
	private TcLawClDetailMapper tcLawClDetailMapper;
	
	
	@Override
	public PageInfo<TcDicLaw> listLawPage(LawCriteriaBean criteria) {
		PageInfo<TcDicLaw> page = PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize())
				.doSelectPageInfo(() -> {
					tcDicLawMapper.listPage(criteria);
				});
		return page;
	}

	@Override
	public TcDicLaw loadLawDetail(Long id) {
		return tcDicLawMapper.selectByPrimaryKey(id);
	}

	@Override
	public TcDicLaw saveUpdateLaw(TcDicLaw law) {
		if (StringUtils.isEmpty(law.getLawCode())) {
			// 新增法规
			Long lawId = RedisKeyHelper.genereateKey(TcDicLaw.class);
			law.setLawId(lawId);
			law.setLawCode(calculateLawCode(law));
			law.setPublishDeptNames(getDeptNames(law.getPublishDeptIds()));
			law.setApprovalDeptNames(getDeptNames(law.getApprovalDeptIds()));
			tcDicLawMapper.insert(law);
		} else {
			// 编辑保存
			law.setLawCode(calculateLawCode(law));// 重新计算法规编码
			law.setPublishDeptNames(getDeptNames(law.getPublishDeptIds()));
			law.setApprovalDeptNames(getDeptNames(law.getApprovalDeptIds()));
			law.setSxId(law.getSxId());
			tcDicLawMapper.updateByPrimaryKey(law);
			// 发布部门
			Example publishDeptExample = new Example(TcLawRelLawPublishDept.class);
			Criteria publishCriteria = publishDeptExample.createCriteria();
			publishCriteria.andEqualTo("lawId", law.getLawId());
			tcLawRelLawPublishDeptMapper.deleteByExample(publishDeptExample);
			// 批准部门
			Example approvalDeptExample = new Example(TcLawRelLawApprovalDept.class);
			Criteria approvalCriteria = approvalDeptExample.createCriteria();
			approvalCriteria.andEqualTo("lawId", law.getLawId());
			tcLawRelLawApprovalDeptMapper.deleteByExample(approvalDeptExample);
		}
		// 保存发布部门、批准部门相关信息
		if (!StringUtils.isEmpty(law.getApprovalDeptIds())) {
			String[] depts = law.getApprovalDeptIds().split(",");
			for (String deptId : depts) {
				TcLawRelLawApprovalDept aDept = new TcLawRelLawApprovalDept();
				aDept.setLawId(law.getLawId());
				aDept.setDeptId(Integer.valueOf(deptId));
				tcLawRelLawApprovalDeptMapper.insert(aDept);
			}
		}
		if (!StringUtils.isEmpty(law.getPublishDeptIds())) {
			String[] depts = law.getPublishDeptIds().split(",");
			for (String deptId : depts) {
				TcLawRelLawPublishDept aDept = new TcLawRelLawPublishDept();
				aDept.setLawId(law.getLawId());
				aDept.setDeptId(Integer.valueOf(deptId));
				tcLawRelLawPublishDeptMapper.insert(aDept);
			}
		}
		return law;
	}

	private String getDeptNames(String publishDeptIds) {
		if (StringUtils.isEmpty(publishDeptIds))
			return null;
		List<String> depts = tcDicLawPublishDeptMapper.findNameByIds(publishDeptIds.split(","));
		StringBuilder deptNames = new StringBuilder();
		boolean b = false;
		for (String dept : depts) {
			if (b)
				deptNames.append(",");
			deptNames.append(dept);
			b = true;
		}
		return deptNames.toString();
	}

	private String calculateLawCode(TcDicLaw law) {
		StringBuilder lawCode = new StringBuilder();
		if (!StringUtils.isEmpty(law.getLawTypeId()) && !StringUtils.isEmpty(law.getLawRegionId())) {
			lawCode.append(String.format("%02d", law.getLawTypeId()));// 法规类型编码
			lawCode.append(law.getLawRegionId());// 法规区划编码
			lawCode.append(String.format("%07d", law.getLawId()));// 流水号
			return lawCode.toString();
		} else {
			return law.getLawCode();
		}
	}

	@Override
	public Long deleteLaw(Long LawId) {
		// 删除相关法规
		tcDicLawMapper.deleteByPrimaryKey(LawId);
		// 案由
		Example lawCauseExample = new Example(TcDicLawCause.class);
		Example.Criteria lawCauseCriteria = lawCauseExample.createCriteria();
		lawCauseCriteria.andEqualTo("lawId", LawId);
		tcDicLawCaseMapper.deleteByExample(lawCauseExample);
		// 条目类别
		Example lawRelCauseItemExample = new Example(TcLawRelCauseLawItem.class);
		Example.Criteria lawRelCauseItemCriteria = lawRelCauseItemExample.createCriteria();
		lawRelCauseItemCriteria.andEqualTo("lawId", LawId);
		tcLawRelCauseLawItemMapper.deleteByExample(lawRelCauseItemExample);
		// 处罚分类
		Example lawRelCauseExcuteExample = new Example(TcLawRelCauseExcuteType.class);
		Example.Criteria lawRelCauseExcuteCriteria = lawRelCauseExcuteExample.createCriteria();
		lawRelCauseExcuteCriteria.andEqualTo("lawId", LawId);
		tcLawRelCauseExcuteTypeMapper.deleteByExample(lawRelCauseExcuteExample);
		// 业务分类
		Example lawRelCauseBizExample = new Example(TcLawRelCauseBiz.class);
		Example.Criteria lawRelCauseBizCriteria = lawRelCauseBizExample.createCriteria();
		lawRelCauseBizCriteria.andEqualTo("lawId", LawId);
		tcLawRelCauseBizMapper.deleteByExample(lawRelCauseBizExample);
		// 职能部门
		Example relPunishUnitFuncExample = new Example(TcRelPunishUnitFunc.class);
		Example.Criteria relPunishUnitFuncCriteria = relPunishUnitFuncExample.createCriteria();
		relPunishUnitFuncCriteria.andEqualTo("lawId", LawId);
		tcRelPunishUnitFuncMapper.deleteByExample(relPunishUnitFuncExample);
		// 职能级别
		Example relPunishUnitLevelExample = new Example(TcRelPunishUnitLevel.class);
		Example.Criteria relPunishUnitLevelCriteria = relPunishUnitLevelExample.createCriteria();
		relPunishUnitLevelCriteria.andEqualTo("lawId", LawId);
		tcRelPunishUnitLevelMapper.deleteByExample(relPunishUnitLevelExample);
		// 处罚阀值
		Example tcLawClDetailExample = new Example(TcLawClDetail.class);
		Example.Criteria tcLawClDetailCriteria = tcLawClDetailExample.createCriteria();
		tcLawClDetailCriteria.andEqualTo("lawId", LawId);
		tcLawClDetailMapper.deleteByExample(relPunishUnitLevelExample);
		return LawId;
	}

	@Override
	public List<Map<String, Object>> lawStat(Long id) {
		List<Map<String, Object>> list = null;
		if(id!=-1){
			list = tcDicLawMapper.statLawByGroupId(id);
		}else {
			list = tcDicLawMapper.statLaw();
		}
		return list;
	}

    @Override
    public List<Map<String, Object>> lawExecuteTypeStat(Long id) {
        List<Map<String, Object>> list = tcDicLawMapper.lawExecuteTypeStat(id);
        return list;
    }

	@Override
	public List<TcDicLaw> getDetail(String code) {
		return tcDicLawMapper.getDetail(code);
	}
}
